/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sorte;

import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Fatec
 */
public class SorteMIDlet extends MIDlet implements CommandListener, ItemStateListener {

    Random random = new Random();
    int n = random.nextInt(5);
    int escolha = 0;
    private boolean midletPaused = false;
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command TentarCommand;
    private Command itemCommand;
    private Command reiniciarCommand;
    private Form Sorteform;
    private StringItem stringItem1;
    private ChoiceGroup escolhachoiceGroup;
    private StringItem resultadostringItem;
    private StringItem sorteadostringItem;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public SorteMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getSorteform());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == Sorteform) {//GEN-BEGIN:|7-commandAction|1|33-preAction
            if (command == TentarCommand) {//GEN-END:|7-commandAction|1|33-preAction
                // write pre-action user code here
                if (n == escolha) {
                    resultadostringItem.setText(" CORRETA ");
                    sorteadostringItem.setText(" " + n);
                } else {
                    resultadostringItem.setText(" ERRADA ");
                    sorteadostringItem.setText(" " + n);
                }
//GEN-LINE:|7-commandAction|2|33-postAction
                // write post-action user code here
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|3|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|4|19-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
        }//GEN-END:|7-commandAction|5|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|6|38-postAction
    //</editor-fold>//GEN-END:|7-commandAction|6|38-postAction


    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Sorteform ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of Sorteform component.
     * @return the initialized component instance
     */
    public Form getSorteform() {
        if (Sorteform == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            Sorteform = new Form("TENTA SORTE !!!", new Item[] { getStringItem1(), getEscolhachoiceGroup(), getResultadostringItem(), getSorteadostringItem() });//GEN-BEGIN:|14-getter|1|14-postInit
            Sorteform.addCommand(getExitCommand());
            Sorteform.addCommand(getTentarCommand());
            Sorteform.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
            Sorteform.setItemStateListener(this);

        }//GEN-BEGIN:|14-getter|2|
        return Sorteform;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    public void itemStateChanged(Item i) {

        try {
            if (escolhachoiceGroup.isSelected(0)) {
                escolha = 1;
            } else if (escolhachoiceGroup.isSelected(1)) {
                escolha = 2;
            } else if (escolhachoiceGroup.isSelected(2)) {
                escolha = 3;

            } else if (escolhachoiceGroup.isSelected(3)) {
                escolha = 4;

            } else if (escolhachoiceGroup.isSelected(4)) {
                escolha = 5;

            }
        } catch (Exception e) {

            resultadostringItem.setText(" ERROR ");
        }

    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem1 ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of stringItem1 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            stringItem1 = new StringItem("Qual valor foi escolhido ? Tenta a Sorte !!", null);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return stringItem1;
    }
    //</editor-fold>//GEN-END:|22-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: escolhachoiceGroup ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initiliazed instance of escolhachoiceGroup component.
     * @return the initialized component instance
     */
    public ChoiceGroup getEscolhachoiceGroup() {
        if (escolhachoiceGroup == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            escolhachoiceGroup = new ChoiceGroup("Qual \u00E9 o seu Palpite ?", Choice.EXCLUSIVE);//GEN-BEGIN:|23-getter|1|23-postInit
            escolhachoiceGroup.append("=> 1", null);
            escolhachoiceGroup.append("=> 2", null);
            escolhachoiceGroup.append("=> 3", null);
            escolhachoiceGroup.append("=> 4", null);
            escolhachoiceGroup.append("=> 5", null);
            escolhachoiceGroup.setSelectedFlags(new boolean[] { false, false, false, false, false });//GEN-END:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return escolhachoiceGroup;
    }
    //</editor-fold>//GEN-END:|23-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: resultadostringItem ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of resultadostringItem component.
     * @return the initialized component instance
     */
    public StringItem getResultadostringItem() {
        if (resultadostringItem == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            resultadostringItem = new StringItem("O seu palpite est\u00E1 ...", null);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return resultadostringItem;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TentarCommand ">//GEN-BEGIN:|32-getter|0|32-preInit
    /**
     * Returns an initiliazed instance of TentarCommand component.
     * @return the initialized component instance
     */
    public Command getTentarCommand() {
        if (TentarCommand == null) {//GEN-END:|32-getter|0|32-preInit
            // write pre-init user code here
            /*
TentarCommand = new Command ("Item", Command.ITEM, 0);//GEN-LINE:|32-getter|1|32-postInit
             */
            TentarCommand = new Command("Tentar", Command.ITEM, 0);

            // write post-init user code here
        }//GEN-BEGIN:|32-getter|2|
        return TentarCommand;
    }
    //</editor-fold>//GEN-END:|32-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: sorteadostringItem ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initiliazed instance of sorteadostringItem component.
     * @return the initialized component instance
     */
    public StringItem getSorteadostringItem() {
        if (sorteadostringItem == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            sorteadostringItem = new StringItem("O valor sorteado foi ...", null);//GEN-LINE:|34-getter|1|34-postInit
            // write post-init user code here
        }//GEN-BEGIN:|34-getter|2|
        return sorteadostringItem;
    }
    //</editor-fold>//GEN-END:|34-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initiliazed instance of itemCommand component.
     * @return the initialized component instance
     */
    public Command getItemCommand() {
        if (itemCommand == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            itemCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|35-getter|1|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return itemCommand;
    }
    //</editor-fold>//GEN-END:|35-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: reiniciarCommand ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of reiniciarCommand component.
     * @return the initialized component instance
     */
    public Command getReiniciarCommand() {
        if (reiniciarCommand == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            /*

reiniciarCommand = new Command ("Item", Command.ITEM, 0);//GEN-LINE:|37-getter|1|37-postInit

             */
            reiniciarCommand = new Command("Reiniciar", Command.ITEM, 0);
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return reiniciarCommand;
    }
    //</editor-fold>//GEN-END:|37-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}
